<template>
  <div>
    <x-Table
      ref="table"
      :loading="loading"
      :table-data="tableData"
      :options-data="options"
      :pagination="pagination"
      :rule-form="ruleForms"
      @handleAdd="handleAdd"
      @handlePaginationCurrentChange="handlePaginationCurrentChange"
      @handlePaginationSizeChange="handlePaginationSizeChange"
      @handleHeardDel="handleHeardDel"
      @searchForm="searchForm"
      @resetForm="resetForm"
      @handleSelectionChange="handleSelectionChange"
    >
    </x-Table>
  </div>
</template>

<script>
let _this;
export default {
  props: {
    selectValue: {
      type: Array,
      default: Array
    },
    twoPagination: {
      type: Object,
      default: Object
    }
  },
  data() {
    return {
      searchParams: {},//搜索对象
      edite: '单选题',
      activeName: '单选题',
      loading: true,
      row: null, //当前选择数据
      ruleForms: {
        title: {label: "题目名称", prop: "title", type: "text"},
        paper_name: {label: "试卷名称", prop: "paper_name", type: "text"},
        type: {
          label: "类型", prop: "types", type: "select", select: [{label: '单选题', value: '单选题'},
            {label: '多选题', value: '多选题'},
            {label: '判断题', value: '判断题'},
            {label: '填空题', value: '填空题'},
            {label: '简答题', value: '简答题'},
            {label: '扫码题', value: '扫码题'},
            {label: '阅读题', value: '阅读题'}]
        },
        pattern: {
          label: "题目类型",
          prop: "pattern",
          type: "select",
          select: [{label: '作业', value: 0}, {label: '考试', value: 1}]
        },
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50, 100],
      }, //分页参数
      otherParam: {
        halfCheckedKeys: [],
      }, //如果有其他情况会拼接这个参数

      dialogVisible: false,
      alertTitle: "添加",
      tableData: [],
      options: {
        buttonEdit: '编辑',
        buttonAdd:'添加',
        fields: {
          id: {label: "ID", prop: "id", width: "120", order: true},
          title: {label: "题目", prop: "title"},
          show_type: {label: "题目显示类型", prop: "show_type"},
          type: {label: "题目类型", prop: "type"},
          pattern: {label: "考试/作业", prop: "pattern"},
          score: {label: "分数", prop: "score", type: 'tag'},
          paper_name: {label: "试卷名称", prop: "paper_name"},
          A: {label: "A", prop: "A"},
          B: {label: "B", prop: "B"},
          C: {label: "C", prop: "C"},
          D: {label: "D", prop: "D"},
          E: {label: "E", prop: "E"},
          F: {label: "F", prop: "F"},
          dan: {label: "单选答案", prop: "dan"},
          duo: {label: "多选答案", prop: "duo"},
          pan: {label: "判断答案", prop: "pan"},
          tian: {label: "填空答案", prop: "tian"},
          content: {label: "简答/扫码答案", prop: "content"},
          json: {label: "阅读答案", prop: "json"},
          create_time: {label: "创建时间", prop: "create_time", order: true},
        },
      },
    };
  },
  methods: {

    resetForm:function (){
      _this.searchParams = {};
    },

    handleSelectionChange: function (val) {
      this.$emit('selectionChangePager', val);
    },
    onClose: function () {
      if (this.$store.state.exam.edit.type == 0) {
        this.dialogVisible = false;
      } else {
        this.$store.commit('exam/setRead', {type: 1, name: ''});
        this.$store.commit('exam/setActionName', '阅读题');
      }
    },

    handleClick: function (tab) {

    },

    readSelectType: function (val) {
      this.$store.commit('exam/setActionName', val.types);
      this.$store.commit('exam/setRead', {type: 1, name: val.types});
    },

    handlePaginationCurrentChange: function (val) {
      _this.loading = true;
      _this.pagination["currentPage"] = val;
      _this.getDateList(_this.searchParams);
      this.$emit('PaginationCurrentChangePaper',val);
    },

    handlePaginationSizeChange: function (val) {
      _this.loading = true;
      _this.pagination["pageSize"] = val;
      _this.getDateList(_this.searchParams);
      this.$emit('PaginationSizeChangePaper',val);
    },

    /**
     * 查询所有专业数据
     */
    getDateList: function (param) {
      _this.$store
        .dispatch("exam/getQuestionList", param ? {
          title: param['title'],
          type: param['types'],
          pattern: param['pattern'],
          paper_name: param['paper_name'],
          page: _this.pagination["currentPage"],
          pageSize: _this.pagination["pageSize"],
        } : {
          page: _this.pagination["currentPage"],
          pageSize: _this.pagination["pageSize"],
        })
        .then(() => {
          _this.loading = false;
          _this.dialogVisible = false;
          let data = this.$store.state.exam.questionList;
          this.tableData = data.data;
          _this.pagination["currentPage"] = data["current_page"];
          _this.pagination["total"] = data["total"];
        });
    },


    /**
     * 批量删除
     */
    handleHeardDel: function (val) {
      // let del = [];
      // val.forEach((item, index) => {
      //   del.push(item.id);
      // });
      // _this.deleteDate(del);
    },


    addDate: function (param) {
      _this.$store
        .dispatch("office/addMajorList", {
          title: param["title"],
          level: param["level"],
          major_code: param["major_code"],
        })
        .then((res) => {
          const {data, msg} = res;
          if (data) {
            _this.$message.success(msg);
            _this.getDateList();
          } else {
            _this.$message.error(msg);
          }
        });
    },

    /**
     * 表单提交回调
     */
    onSubmit: function (param) {


    },

    /**
     * 搜索角色标题
     */
    searchForm: function (val) {
      _this.searchParams = val;
      _this.loading = true;
      _this.getDateList(val);
    },
    /**
     * 更新角色
     */
    updateDate: function (param) {
      _this.$store
        .dispatch("office/updateMajorList", {
          id: _this.row["id"],
          title: param["title"],
          level: param["level"],
          major_code: param["major_code"],
        })
        .then((res) => {
          const {data, msg} = res;
          if (data) {
            _this.getDateList();
            _this.$message.success(msg);
            return;
          }
          return _this.$message.error(msg);
        });
    },

    /**
     * 删除角色
     */
    deleteDate: function (val) {
      _this.$store
        .dispatch("exam/deleteQuestion", {
          id: val,
        })
        .then((res) => {
          const {data, msg} = res;
          if (data) {
            _this.getDateList();
            _this.$message.success(msg);
            return;
          }
          return _this.$message.error(msg);
        });
    },

    /**
     *  表单导航栏添加点击
     */
    handleAdd: function () {
      _this.alertTitle = "添加";
      _this.$store.commit('exam/setRead', {type: 0, name: ''})
      _this.$store.commit('exam/setActionName', '单选题');
      _this.dialogVisible = true;
      _this.row = null;
    },
  },

  created() {
    _this = this;
    this.pagination.currentPage = this.twoPagination.currentPage;
    this.pagination.pageSize = this.twoPagination.pageSize;
    this.getDateList();
    this.$nextTick(() => {
      this.$refs.table.toggleSelection(_this.selectValue);
    })
  }

};
</script>

<style scoped>

</style>
